package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskProductsMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskConfigurationItems;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskProducts;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskProductsService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskProductsServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskProductsMapper, TaskAutotaskProducts>
                implements TaskAutotaskProductsService {
    @Override
    public void sync(ResultEntity content){
        log.debug("Auto task products receiver content {}", content);
        JsonNode entity = content.getEntity();
        TaskAutotaskProducts data = TaskAutotaskProducts.builder()
                .productsId(super.getLongValue(entity.get("id")))
                .markupRate(ObjectUtils.isEmpty(entity.get("markupRate")) ? null : entity.get("markupRate").asDouble())
                .msrp(ObjectUtils.isEmpty(entity.get("msrp")) ? null : entity.get("msrp").asDouble())
                .unitCost(ObjectUtils.isEmpty(entity.get("unitCost")) ? null : entity.get("unitCost").asDouble())
                .unitPrice(ObjectUtils.isEmpty(entity.get("unitPrice")) ? null : entity.get("unitPrice").asDouble())
                .billingType(ObjectUtils.isEmpty(entity.get("billingType")) ? null : entity.get("billingType").asInt())
                .chargeBillingCodeId(ObjectUtils.isEmpty(entity.get("chargeBillingCodeID")) ? null : entity.get("chargeBillingCodeID").asInt())
                .defaultVendorId(ObjectUtils.isEmpty(entity.get("defaultVendorID")) ? null : entity.get("defaultVendorID").asInt())
                .impersonatorCreatorResourceId(ObjectUtils.isEmpty(entity.get("impersonatorCreatorResourceID")) ? null : entity.get("impersonatorCreatorResourceID").asInt())
                .periodType(ObjectUtils.isEmpty(entity.get("periodType")) ? null : entity.get("periodType").asInt())
                .priceCostMethod(ObjectUtils.isEmpty(entity.get("priceCostMethod")) ? null : entity.get("priceCostMethod").asInt())
                .productBillingCodeId(ObjectUtils.isEmpty(entity.get("productBillingCodeID")) ? null : entity.get("productBillingCodeID").asInt())
                .productCategory(ObjectUtils.isEmpty(entity.get("productCategory")) ? null : entity.get("productCategory").asInt())
                .description(ObjectUtils.isEmpty(entity.get("description"))?null : entity.get("description").asText())
                .externalProductId(ObjectUtils.isEmpty(entity.get("externalProductID"))?null : entity.get("externalProductID").asText())
                .internalProductId(ObjectUtils.isEmpty(entity.get("internalProductID"))?null : entity.get("internalProductID").asText())
                .link(ObjectUtils.isEmpty(entity.get("link"))?null : entity.get("link").asText())
                .manuFacturerName(ObjectUtils.isEmpty(entity.get("manufacturerName"))?null : entity.get("manufacturerName").asText())
                .manuFacturerProductName(ObjectUtils.isEmpty(entity.get("manufacturerProductName"))?null : entity.get("manufacturerProductName").asText())
                .name(ObjectUtils.isEmpty(entity.get("name"))?null : entity.get("name").asText())
                .sku(ObjectUtils.isEmpty(entity.get("sku"))?null : entity.get("sku").asText())
                .vendorProductNumber(ObjectUtils.isEmpty(entity.get("vendorProductNumber"))?null : entity.get("vendorProductNumber").asText())
                .doesNotRequireProcurement(ObjectUtils.isEmpty(entity.get("doesNotRequireProcurement")) || entity.get("doesNotRequireProcurement").asBoolean())
                .isActive(ObjectUtils.isEmpty(entity.get("isActive")) || entity.get("isActive").asBoolean())
                .isEligibleForRma(ObjectUtils.isEmpty(entity.get("isEligibleForRma")) || entity.get("isEligibleForRma").asBoolean())
                .isSerialized(ObjectUtils.isEmpty(entity.get("isSerialized")) || entity.get("isSerialized").asBoolean())
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());

        TaskAutotaskProducts dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskProducts>lambdaQuery()
                .eq(TaskAutotaskProducts::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskProducts::getProductsId, data.getProductsId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }
}
